/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2022-05-17     fanlulin       the first version
 */



#include "key.h"

static rt_thread_t key = RT_NULL;
/* control led by the key */
void key1_callback(void *args)
{
    rt_kprintf("turn on the led!\n");
    rt_pin_write(LEDR_PIN, PIN_LOW);
}

void key2_callback(void *args)
{
    rt_kprintf("turn off the led!\n");
    rt_pin_write(LEDR_PIN, PIN_HIGH);
}

int key_entry(void)
{

    //绑定KEY1_PIN引脚中断回调函数
    rt_pin_attach_irq(KEY1_PIN, PIN_IRQ_MODE_FALLING, key1_callback, RT_NULL);
    //使能引脚中断
    rt_pin_irq_enable(KEY1_PIN, PIN_IRQ_ENABLE);



    //绑定KEY2_PIN引脚中断回调函数
    rt_pin_attach_irq(KEY2_PIN, PIN_IRQ_MODE_FALLING, key2_callback, RT_NULL);
    //使能引脚中断
    rt_pin_irq_enable(KEY2_PIN, PIN_IRQ_ENABLE);

    return 0;
}


int key_thread(void){
    key = rt_thread_create("key",
                            key_entry, RT_NULL,
                            THREAD_STACK_SIZE,
                            THREAD_PRIORITY, THREAD_TIMESLICE);
    if (key != RT_NULL){
        rt_thread_startup(key);
        rt_kprintf("key thread start successfully\r\n");
    }

    else{
        rt_kprintf("key thread start failed\r\n");
    }
    return 0;
}

